package com.sky.controller.admin;

import com.sky.constant.RedisKeyConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className SetmealController.java
 * @date 2024 01 21
 */
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api("套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    Result<PageResult> page( SetmealPageQueryDTO setmealPageQueryDTO){

        PageResult pageResult =  setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 套餐添加接口
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.SETMEAL,key = "#setmealDTO.categoryId")
    @PostMapping
    @ApiOperation("套餐添加")
    public Result<String> save(@RequestBody SetmealDTO setmealDTO){
        log.info("添加套餐信息为{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
         SetmealVO setmealVO =  setmealService.getById(id);
         log.info("套餐信息为{}",setmealVO);
         return Result.success(setmealVO);
    }

    /**
     * 套餐信息修改
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames=RedisKeyConstant.SETMEAL,key = "#setmealDTO.categoryId")
    @ApiOperation("修改套餐信息")
    public Result<String > update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息为{}",setmealDTO);
       setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐的起售和停售
     * @param status
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.SETMEAL,allEntries = true)
    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    public Result<String > status(@PathVariable Integer status,Long id){
        setmealService.status(status,id);
        return Result.success();
    }

    /**
     * 套餐批量删除
     * @return
     */
    @CacheEvict(cacheNames = RedisKeyConstant.SETMEAL,allEntries = true)
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result<String > deleteByIds(@RequestParam List<Long> ids){
        log.info("删除的套餐id为{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }
}
